import { _decorator, Component, Node, AudioSource, director } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AudioManager')
export class AudioManager {
    private static _instance: AudioManager = null;
    static getInstance() {
        if (this._instance === null) {
            this._instance = new AudioManager();
        }
        return this._instance;
    }

    /** AudioSource 挂载在此节点上 */
    private attachNode: Node = null;
    /** AudioSource 组件 */
    private audioSource: AudioSource = null;

    private constructor() {
        this.attachNode = director.getScene().getChildByName("Canvas");
        this.audioSource = this.attachNode.addComponent(AudioSource);
    }

    play(bundleName: string, audioPath: string, loop: boolean = true) {
        gCtr.res.loadAudioClip(bundleName, audioPath, (audioClip) => {
            let audioSource = this.audioSource;
            audioSource.clip = audioClip;
            audioSource.loop = loop;
            audioSource.play();
        });
    }


}


